<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Flutter最佳实践整理4》 - 飞雪无情的博客</title><meta name="Description" content="专注于IT互联网，包括但不限于Go语言(golang)、Java、Android、Python、项目管理、抖音分析、软件架构等"><meta property="og:title" content="《Flutter最佳实践整理4》" />
<meta property="og:description" content="
本文主要介绍
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.flysnow.org/posts/flutter/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%864/" />
<meta property="article:published_time" content="2021-07-30T22:38:23+08:00" />
<meta property="article:modified_time" content="2021-07-30T22:38:23+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="《Flutter最佳实践整理4》"/>
<meta name="twitter:description" content="
本文主要介绍
"/>
<meta name="application-name" content="飞雪无情的博客">
<meta name="apple-mobile-web-app-title" content="飞雪无情的博客"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.flysnow.org/posts/flutter/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%864/" /><link rel="prev" href="https://www.flysnow.org/posts/flutter/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%863/" /><link rel="next" href="https://www.flysnow.org/posts/flutter/flutter%E7%B2%BE%E5%8D%8E%E8%B5%84%E6%96%99%E6%95%B4%E7%90%86/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Flutter最佳实践整理4》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.flysnow.org\/posts\/flutter\/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%864\/"
        },"genre": "posts","keywords": "flutter","wordcount":  363 ,
        "url": "https:\/\/www.flysnow.org\/posts\/flutter\/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%864\/","datePublished": "2021-07-30T22:38:23+08:00","dateModified": "2021-07-30T22:38:23+08:00","publisher": {
            "@type": "Organization",
            "name": "飞雪无情"},"author": {
                "@type": "Person",
                "name": "飞雪无情"
            },"description": ""
    }
    </script></head>
    <body header-desktop="" header-mobile=""><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><div class="logo-wrapper">
  <a href="/%20/" class="logo">飞雪无情的博客</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/tools/">工具</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/archives/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于</a>
    </li>
  </ul>
</nav><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">Contents</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Flutter最佳实践整理4》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>飞雪无情</a></span>&nbsp;<span class="post-category">included in <a href="/categories/Flutter/"><i class="far fa-folder fa-fw"></i>Flutter</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-07-30">2021-07-30</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;363 words&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;One minute&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>Contents</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#平台相关的判断">平台相关的判断</a></li>
    <li><a href="#scrollview-滑动隐藏键盘">ScrollView 滑动隐藏键盘</a></li>
    <li><a href="#回调写法">回调写法</a>
      <ul>
        <li><a href="#a无参数回调">a.无参数回调</a></li>
        <li><a href="#b有一个参数回调">b.有一个参数回调</a></li>
        <li><a href="#c参数大于一个">c.参数大于一个</a></li>
      </ul>
    </li>
    <li><a href="#flutter-pub-get-is-stuck">flutter pub get is stuck</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍</p>
</blockquote>
<h2 id="平台相关的判断">平台相关的判断</h2>
<p>只关心是否是iOS和Android的情况下不需要依赖<code>context</code>，优先使用<code>Platform</code></p>
<pre><code>Platform.isAndroid
Platform.isIOS
</code></pre><p>需要详细知道具体哪个平台才使用<code>TargetPlatform</code></p>
<p>这个API的缺点是需要依赖<code>context</code>这个参数</p>
<pre><code>final platform = Theme.of(context).platform;

if (platform == TargetPlatform.android) {
	...
} else if (platform == TargetPlatform.iOS) {
	...
}
</code></pre><h2 id="scrollview-滑动隐藏键盘">ScrollView 滑动隐藏键盘</h2>
<p><a href="https://stackoverflow.com/questions/55306855/hide-keyboard-on-scroll-in-flutter">https://stackoverflow.com/questions/55306855/hide-keyboard-on-scroll-in-flutter</a></p>
<p>使用ScrollView的<code>keyboardDismissBehavior</code>属性</p>
<pre><code>ListView(
	keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag
)
</code></pre><h2 id="回调写法">回调写法</h2>
<h3 id="a无参数回调">a.无参数回调</h3>
<p>VoidCallback</p>
<h3 id="b有一个参数回调">b.有一个参数回调</h3>
<p>ValueChanged</p>
<h3 id="c参数大于一个">c.参数大于一个</h3>
<p>可以通过<code>typedef</code>自定义一个函数</p>
<p>下面是Example用法</p>
<pre><code>final VoidCallback onPressed;
final ValueChanged&lt;T&gt; onSelectHandler;
typedef ImageSwiperOnTap = void Function(int index, List&lt;String&gt; imgUrls);
</code></pre><h2 id="flutter-pub-get-is-stuck">flutter pub get is stuck</h2>
<p>可以通过切换flutter镜像到中文站点来解决</p>
<p>使用系统shell，请编辑</p>
<p>使用oh_my_zsh, 需要编辑<code>.zshrc</code></p>
<pre><code>export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
</code></pre><p>保存文件后，关闭，下次重新打开终端生效</p>
<p>再执行<code>flutter pub get</code> 查看速度是否正常</p></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>Updated on 2021-07-30</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/flutter/">flutter</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">Back</a></span>&nbsp;|&nbsp;<span><a href="/">Home</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/flutter/flutter%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E6%95%B4%E7%90%863/" class="prev" rel="prev" title="《Flutter最佳实践整理3》"><i class="fas fa-angle-left fa-fw"></i>《Flutter最佳实践整理3》</a>
            <a href="/posts/flutter/flutter%E7%B2%BE%E5%8D%8E%E8%B5%84%E6%96%99%E6%95%B4%E7%90%86/" class="next" rel="next" title="《Flutter精华资料整理》">《Flutter精华资料整理》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">Powered by <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> | Theme - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">飞雪无情</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="Back to Top">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="View Comments">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"Copy to clipboard","maxShownLines":10},"comment":{}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
